<style lang="less">
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card>
                    <Row>
                        <Form ref="searchForm" :model="searchForm" inline :label-width="160" class="search-form">
                            <Row class="operation">
                                <i-col span="8">
                                    <Form-item label="问题类型" prop="problemType">
                                        <Select v-model="searchForm.problemType" placeholder="请选择" clearable style="width: 200px">
                                            <Option :value="item.value" v-for="(item,index) in problemTypeStatusList" :key="index"> {{item.title}}</Option>
                                        </Select>
                                    </Form-item>
                                </i-col>
                            </Row>

                            <Row  style="text-align: center">
                                <Form-item style="margin-left:-60px; text-align: center" class="br">
                                    <Button @click="handleSearch" type="primary" icon="ios-search">查询</Button>
                                    <Button @click="handleReset" icon="md-redo">重置</Button>
                                </Form-item>
                            </Row>

                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button @click="add" type="primary" icon="md-add">新增</Button>
                        <Button @click="delAll" icon="md-trash">批量删除</Button>
                        <circleLoading v-if="operationLoading"/>
                    </Row>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom"
                               @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table">
                            <template slot-scope="{ row, index }" slot="action">
                                <Button style="margin-right:5px" type="primary" size="small" @click="edit(row)">编辑</Button>
                            </template>
                            <template slot-scope="{ row, index }" slot="problemType">
                                <span>{{setProblemTypeTitle(row.problemType)}}</span>
                            </template>
                        </Table>
                    </Row>

                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize"
                              @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                              size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <Modal :title="modalTitle" v-model="problemModalVisible" :mask-closable='false' :width="600"
               :styles="{top: '90px'}">
            <Form ref="problemForm" :model="problemForm" :label-width="90" :rules="problemFormValidate">
                <FormItem label="问题类型" prop="problemType">
                    <Select v-model="problemForm.problemType" placeholder="请选择" clearable style="width: 200px">
                        <Option :value="item.value" v-for="(item,index) in problemTypeStatusList" :key="index"> {{item.title}}</Option>
                    </Select>
                </FormItem>
                <FormItem label="问题" prop="problem">
                    <Input v-model="problemForm.problem" clearable  class="w400" maxlength="300" placeholder="请输入" type="textarea" :rows="6"/>
                </FormItem>
                <FormItem label="答案" prop="answer" >
                    <Input v-model="problemForm.answer" clearable  class="w400"  maxlength="300" placeholder="请输入" type="textarea" :rows="6"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancel">取消</Button>
                <Button type="primary" :loading="saveLoading" @click="saveProblem">保存</Button>
            </div>
        </Modal>
        <Modal :title="modalUploadTitle" v-model="uploadModalVisible" :mask-closable='false' :width="1200"  :styles="{top: '90px'}">
            <!--附件-->
            <file-operation v-if="uploadModalVisible" :uploadParam="uploadParam" :isInt="canUpload" ></file-operation>
            <div slot="footer">
                <Button type="text" @click="cancelUpload">取消</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import {
        getAppCommonProblem,
        saveProblem,
        delByIds
    } from "@/projects/afs-apply/api/app/app";
    import {getDictDataByType} from "_p/basic/api/admin/datadic";
    import FileOperation from "_p/basic/pages/image/upload-file/fileOperation.vue";

    export default {
        name: "commonProblem",
        components: {
        },
        data() {
            return {
                loading: true,
                drop: false,
                selectCount: 0,
                selectList: [],
                operationLoading: false,
                modalTitle: "",
                modalUploadTitle:"",
                uploadModalVisible:false,
                problemModalVisible:false,
                saveLoading:false,
                canUpload:false,
                searchForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    problemType:""
                },
                uploadParam: {
                    busiNo: "",
                    belongNo: "",
                    busiType: "",
                    busiData: {}
                },
                //问题类型字典处理
                problemTypeStatusList:[],
                problemForm :{
                    problem:"",
                    problemType:"",
                    answer:""
                },
                problemFormValidate:{
                    answer: [
                        { required: true, message: "答案不能为空", trigger: "blur"}
                    ],
                    problem: [
                        { required: true, message: "问题不能为空", trigger: "blur"}
                    ],
                    problemType: [
                        { required: true, message: "问题类型不能为空", trigger: "change"}
                    ],
                },
                columns: [
                    {
                        type: "selection",
                        minWidth: 60,
                        maxWidth:60,
                        align: "center",
                        fixed: "left"
                    },
                    {
                        type: 'index',
                        title:"序号",
                        minWidth: 60,
                        fixed: "left",
                    },
                    {
                        title: "问题类型",
                        key: "problemType",
                        minWidth: 100,
                        align: "center",
                        slot:"problemType"
                    },
                    {
                        title: "问题",
                        key: "problem",
                        minWidth: 300,
                        align: "center",
                    },
                    {
                        title: "答案",
                        key: "answer",
                        align: "center",
                        minWidth: 300
                    },
                    {
                        title: "操作",
                        key: "action",
                        width: 80,
                        align: "center",
                        fixed: "right",
                        slot: "action"
                    }
                ],
                data: [],
                total: 0
            };
        },
        methods: {
            init() {
                this.getAppCommonProblem();
                this.initLabelType();
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getAppCommonProblem();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getAppCommonProblem();
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            getAppCommonProblem() {
                // 多条件app常见问题列表
                this.loading = true;
                getAppCommonProblem(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.data = res.data.records;
                        this.total = res.data.total;
                    }
                });
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getAppCommonProblem();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                // 重新加载数据
                this.getAppCommonProblem();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getAppCommonProblem();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "更多";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            //常见问题字典处理
            setProblemTypeTitle(val){
                if(val==null ||val=="" ||val ==undefined){
                    return;
                }
                let dic = {};
                this.problemTypeStatusList.forEach(column => {
                    if(column.value === val){
                        dic = column;
                    }
                });
                return dic.title;
            },

            initLabelType(){
                let problemTypeDic="problemType";
                getDictDataByType(problemTypeDic).then(res =>{
                    if (res.code === "0000"){
                        this.problemTypeStatusList = res.data;
                    }
                });
            },
            add() {
                this.modalType = 0;
                this.modalTitle = "新增常见问题";
                this.$refs.problemForm.resetFields();
                this.problemForm.id=null;
                this.problemForm.createTime=null;
                this.problemModalVisible = true;

            },
            edit(v) {
                this.modalType = 1;
                this.modalTitle = "编辑常见问题";
                this.$refs.problemForm.resetFields();
                // 转换null为""
                for (let attr in v) {
                    if (v[attr] === null) {
                        v[attr] = "";
                    }
                }
                let str = JSON.stringify(v);
                let applyAppCommonProblem = JSON.parse(str);
                this.problemForm = applyAppCommonProblem;
                this.problemModalVisible = true;
            },
            saveProblem(){
                this.$refs.problemForm.validate(valid => {
                    if (valid) {
                        this.saveLoading = true;
                        saveProblem(this.problemForm).then(res => {
                            this.saveLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("保存成功");
                                this.getAppCommonProblem();
                                this.problemModalVisible = false;
                            }
                        }).catch(() => {
                            this.saveLoading = false;
                        });
                    }

                });
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = [];
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        this.operationLoading = true;
                        delByIds(ids).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.clearSelectAll();
                                this.getAppCommonProblem();
                            }
                        });
                    }
                });
            },
            cancel() {
                //关闭窗口
                this.problemModalVisible = false;
            },
            cancelUpload(){
                //关闭窗口
                this.uploadModalVisible = false;
                this.getAppCommonProblem();
            },
            upload(v){
                this.canUpload=true;
                this.uploadModalVisible = true;
                this.uploadParam.busiNo = v.id;
                this.uploadParam.belongNo = v.id;
                this.uploadParam.busiData = {};
                this.uploadParam.busiType = "appPictureManagement";

            },
        },
        components:{
            //注入子组件
            FileOperation
        },
        created() {

        },
        mounted() {
            this.init();
        },
    };
</script>
<style scoped>
</style>
